package com.three.lab.page.address;


import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.donkingliang.labels.LabelsView;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.style.cityjd.JDCityConfig;
import com.lljjcoder.style.cityjd.JDCityPicker;
import com.suke.widget.SwitchButton;
import com.three.lab.R;
import com.three.lab.base.AppConstant;
import com.three.lab.base.BaseActivity;
import com.three.lab.bean.AddressBean;
import com.three.lab.util.ToastUtils;
import com.three.lab.util.XpopupUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 地址编辑
 * created by lqd on 2024-06-16.
 */
public class AddressEditActivity extends BaseActivity {

    private TextView tvTitle,tvArea;
    private ImageView ivRight;
    private AddressBean bean = null;

    private EditText etName,etPhone,etAddress;
    private LabelsView labelsView;
    private SwitchButton switchButton;
    private boolean isDefault = false;

    @Override
    protected void initParms(Bundle bundle) {
        if (bundle != null) {
            bean = bundle.getParcelable(AppConstant.EXTRAS_BEAN);
        }
    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_address_edit;
    }

    @Override
    protected void initViews(View view, Bundle savedInstanceState) {
        tvTitle = $(R.id.tv_title);
        ivRight = $(R.id.iv_right);

        tvArea = $(R.id.tv_area);
        etName = $(R.id.et_name);
        etPhone = $(R.id.et_phone);
        etAddress = $(R.id.et_address);
        switchButton = $(R.id.switch_button);

        labelsView = $(R.id.labels);

    }

    @Override
    protected void setListener() {
        $(R.id.iv_left).setOnClickListener(this);
        $(R.id.iv_right).setOnClickListener(this);
        $(R.id.ll_area).setOnClickListener(this);
        $(R.id.submit).setOnClickListener(this);
        switchButton.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {
                isDefault = isChecked;
            }
        });
//        labelsView.setOnLabelSelectChangeListener(new LabelsView.OnLabelSelectChangeListener() {
//            @Override
//            public void onLabelSelectChange(TextView label, Object data, boolean isSelect, int position) {
//                Log.i("TAG","22222222=" + data + "isSelect=" + isSelect);
//            }
//        });
//        labelsView.setOnLabelClickListener(new LabelsView.OnLabelClickListener() {
//            @Override
//            public void onLabelClick(TextView label, Object data, int position) {
//                Log.i("TAG","444=" + data);
//            }
//        });

    }

    @Override
    protected void widgetClick(View v) {
        switch (v.getId()){
            case R.id.iv_left:
                finish();
                break;
            case R.id.iv_right:
                onDelete();
                break;
            case R.id.ll_area:
                onPickArea();
                break;
            case R.id.submit:
                onSubmit();
                break;
        }
    }

    private void onPickArea() {
        JDCityPicker cityPicker = new JDCityPicker();
        JDCityConfig jdCityConfig = new JDCityConfig.Builder().build();

        jdCityConfig.setShowType(JDCityConfig.ShowType.PRO_CITY_DIS);
        cityPicker.init(this);
        cityPicker.setConfig(jdCityConfig);
        cityPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                tvArea.setText("" + province.getName() + city.getName() + district.getName());
            }

            @Override
            public void onCancel() {
            }
        });
        cityPicker.showCityPicker();
    }

    private void onSubmit() {
        String name = etName.getText().toString();
        if (TextUtils.isEmpty(name)) {
            ToastUtils.showToast("请填写收货人名字");
            return;
        }
        String phone = etPhone.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            ToastUtils.showToast("请填写收货人联系电话");
            return;
        }
        String area = tvArea.getText().toString();
        if (TextUtils.isEmpty(area)) {
            ToastUtils.showToast("请选择所在地区");
            return;
        }
        String address = etAddress.getText().toString();
        if (TextUtils.isEmpty(address)) {
            ToastUtils.showToast("请填写收货人详细地址");
            return;
        }
        List<String> label = labelsView.getSelectLabelDatas();

        showLoading("正在提交...");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                hideLoading();
                ToastUtils.showToast("提交成功！");
                finish();
            }
        },500);
    }

    private void onDelete() {
        XpopupUtil.showOnce(this, "您确定要删除吗？", new XpopupUtil.OnPopupOkListener() {
            @Override
            public void onPopupConfirm() {
                onDeleting();
            }
        });
    }

    private void onDeleting() {
        showLoading("正在删除...");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                hideLoading();
                ToastUtils.showToast("删除成功！");
                finish();
            }
        },500);
    }

    @Override
    protected void initData(Context mContext) {
        ArrayList<String> labelList = new ArrayList<>();
        labelList.add("家");
        labelList.add("公司");
        labelList.add("住宅");
        labelsView.setLabels(labelList); //直接设置一个字符串数组就可以了。

        if (bean == null) {
            tvTitle.setText("新建");
        } else {
            tvTitle.setText("编辑");
            ivRight.setVisibility(View.VISIBLE);
            ivRight.setImageResource(R.mipmap.iv_delete);

            etName.setText(bean.getName());
            etAddress.setText(bean.getAddress());
            etPhone.setText(bean.getPhone());
            switchButton.setChecked(bean.isDefault());
            tvArea.setText(bean.getProvince() + bean.getCity() + bean.getArea());

            String label = bean.getLabel();
            int position = labelList.indexOf(label);
            labelsView.setSelects(position);
        }
    }
}